$(document).ready(function() {
	$("form[name='newRepair']").submit(addRepair);
	$(".filterForm :checkbox[name='applied']").click(function() {
		var checked = $(this).attr("checked");
		if (checked) {
			$(".filterForm :checkbox[name='processed']").attr("checked", "checked")
														.attr("disabled", "disabled");
		} else {
			$(".filterForm :checkbox[name='processed']").removeAttr("disabled");
		}
	});
	
	$("#repairs :checkbox[name='checkAll']").click(function() {
		var checked = $(this).attr("checked");
		var itemsCheckbox = $("#repairs :checkbox:enabled[name='item']");
		if (checked) {
			itemsCheckbox.attr("checked", "checked");
		} else {
			itemsCheckbox.removeAttr("checked");
		}
	});
	//初始化modifyDialog
	$(".modifyDialog").dialog({
		autoOpen : false,
		modal : true,
		buttons : {
			"取消" : function() {
				$(this).dialog("close");
			}
		}
	});
	
	$("#repairs :button[name='modify']").click(handleModify);
	$(":button[name='delete']").click(function() {
		var confirmed = confirm("你确定要删除吗？");
		if (confirmed) {
			deleteRepairs();
		} else {
			return false;
		}
	});
});

function addRepair() {
	var newRepairForm = $(this);
	var submitButton = newRepairForm.find(":submit[name='submit']");	
	var title = newRepairForm.find(":text[name='title']").val();
	var reason = newRepairForm.find("textarea[name='reason']").val();
	if (title.length == 0 || reason.length == 0) {
		alert("标题和原因必须填写")
		return false;
	}
	submitButton.attr("disabled", "disabled");
	$.ajax({
		type : "POST",
		dateType : "json",
		data : {
			"title" : title,
			"reason" : reason
		},
		url : "addRepair.do",
		async : false,
		success : function (json, textStatus) {
			if (typeof json == "string") {
				var jsonData = eval("(" + json + ")");
			} else {
				jsonData = json;
			}
			if (jsonData.status) {
				//先destroy,切记
				$(".messageDialog").dialog("destroy");
				$(".messageDialog").text(jsonData.message);
				$(".messageDialog").dialog({
					modal : true,
					autoOpen : true,
					buttons : {
						"查看保修列表" : function() {location.href = "repairList.do";},
						"继续添加" : function() {
							$(this).dialog("close");
							newRepairForm.find(":text[name='title']").val("");
							newRepairForm.find("textarea[name='reason']").val("");
							submitButton.removeAttr("disabled");
						}
					}
				});
			} else {
				$(".ajaxMessage").text(jsonData.message).show();
				submitButton.removeAttr("disabled");
			}
		},
		error : function() {
			$(".ajaxMessage").text("请求失败，请稍后再试");
			submitButton.removeAttr("disabled");
		}
	});
	return false;
}

function handleModify() {
	var modifyDialog = $(".modifyDialog");
	var tr = $(this).parents("tr");
	var updatable = !(tr.find(":checkbox[name='processed']").attr("checked"));
	var repairId = tr.find("td[title='id']").text();
	var title = tr.find("td[title='title']").text();
	var reason = tr.find("td[title='reason']").text();
	
	modifyDialog.find(":text[name='title']").val(title);
	modifyDialog.find(":textarea[name='reason']").val(reason);
	modifyDialog.find(":hidden[name='repairId']").val(repairId);
	
	if (updatable) {
		modifyDialog.dialog("option", "buttons", 
				{
					"取消" : function() {
					
						$(this).dialog("close");
					},
					"更新" : function() {
						//更新这个repair
						updateRepair(tr);
					}
				}
		);
	} else {
		modifyDialog.dialog("option", "buttons", 
				{
					"取消" : function() {
						$(this).dialog("close");
					}
				}
		);
	}
	
	modifyDialog.dialog("open");
}

function updateRepair(tr) {
	var modifyDialog = $(".modifyDialog");
	var title = modifyDialog.find(":text[name='title']").val();
	var reason = modifyDialog.find("textarea[name='reason']").val();
	var repairId = modifyDialog.find(":hidden[name='repairId']").val();
	if (title.length == 0 || reason.length == 0) {
		alert("标题和原因不能为空");
		return;
	}
	
	var repair = {
			"id" : repairId,
			"title" : title,
			"reason" : reason
	};
	
	$.ajax({
		async : true,
		type : "POST",
		data : repair,
		dataType : "json",
		url : "updateRepair.do",
		success : function(json, textStatus) {
			var jsonData = json;
			if (jsonData.status) {
				tr.find("td[title='title']").text(repair.title);
				tr.find("td[title='reason']").text(repair.reason);
				$(".ajaxMessage").text("更新成功...").show();					
			} else {
				$(".ajaxMessage").text("更新失败，请稍候再试...").show();
			}			
		},
		error : function() {
			$(".ajaxMessage").text("更新失败，请稍候再试...").show();
		}
	});
	modifyDialog.dialog("close");
}

function deleteRepairs() {
	var repairIds = new Array();
	var rows = new Array();
	
	$("#repairs :checkbox:checked[name='item']").each(function() {
		var id = $(this).val();
		repairIds.push(id);
		var tr = $(this).parents("tr");
		rows.push(tr);
	});
	
	$.ajax({
		cache : false,
		async : false,
		data : {
			"repairIds" : repairIds
		},
		url : "deleteRepair.do",
		dataType : "json",
		success : function(json, textStatus) {
			var jsonData = json;
			if (jsonData.status) {
				$(".ajaxMessage").text("删除成功...").show();
				//一秒后刷新页面
				setTimeout(function() {location.reload(true);}, 1000);
			} else {
				$(".ajaxMessage").text("删除失败,请稍后再试...").show();
			}
		},
		error : function() {
			$(".ajaxMessage").text("删除失败，请稍后再试...").show();
		}
	});
}


